﻿using System.Collections.Generic;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;

namespace AgencyManagerSystem.DataUpdater
{
    public class RetailMemberUpdater : BaseUpdater<RetailMemberModel>
    {
        public RetailMemberUpdater(RetailMemberFetcher fetcher) : base(fetcher) { }
        public virtual bool UpdateForRetail(int retail, IEnumerable<RetailMemberModel> data,out string message)
        {
            foreach (var item in data)
            {
                item.Retail = retail;
            }
            RetailMemberFetcher suppilerlMemberFetcher = new RetailMemberFetcher();
            var members = suppilerlMemberFetcher.ListByRetail(retail);
            var split = ListSplit(data, members);
            foreach (var item in split[0])
            {
                var objId = Save(item, out message);
                if (objId == 0)
                    return false;
            }
            foreach (var item in split[1])
            {
                var objId = Save(item, out message);
                if (objId == 0)
                    return false;
            }
            foreach (var item in split[2])
            {
                var succ = Delete(item, out message);
                if (!succ)
                    return false;
            }
            message = "操作成功";
            return true;
        }
    }
}

